<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link
      href="https://cdn.bootcss.com/twitter-bootstrap/4.2.1/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <script src="./debug.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/vue/2.6.3/vue.min.js"></script>
  </head>
  <body>
    <div id="filter">
      <div v-for=""></div>
    </div>
    <script>
      var api_host = "http://139.196.79.232:8089";
      if (DEBUG) api_host = "http://local.tim.io/test.php?r=";
      var api = {
        getAll: api_host + "/v1/level_attribute/pid/0",
        getSub: api_host + "/v1/level_attribute/pid/",
        crete: api_host + "/v1/level_attribute/create",
        update: api_host + "/v1/level_attribute/104/update",
        delete: ""
      };
      var app;
      app = new Vue({
        el: "#filter",
        data: {
          lists: {}
        },
        created: function() {
          this.init();
        },
        methods: {
          array2dic: function(array, key) {
            var dic = {};
            for (var i in array) {
              dic[array[i][key]] = array[i];
            }
            return dic;
          },
          init: function() {
            var that = this;
            $.get(api.getAll, function(res) {
              console.log(res);
              if (DEBUG) {
                that.lists = that.array2dic(moke.getAll, "id");
                return;
              }
              if (res.errcode == 0) {
                that.lists = that.array2dic(res.data, "id");
                for (var index in res.data) {
                  let item = res.data[index];
                  var pid = item.id;
                  $.get(api.getSub + pid, function(res) {
                    if (DEBUG) {
                      that.lists[pid]["child"] = that.array2dic(
                        moke.getSub[pid],
                        "id"
                      );
                      return;
                    }
                    if (res.errcode == 0) {
                      that.lists[pid]["child"] = that.array2dic(res.data, "id");
                      console.log(that.lists);
                    } else {
                      // 错误提示
                    }
                  });
                }
              } else {
                // 错误提示
              }
            });
          }
        }
      });
    </script>
  </body>
</html>
